package com.hl.apple.myapplication

import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView
import android.text.Editable
import android.text.TextWatcher
import android.util.Log
import android.view.View
import android.widget.TextView
import com.chad.library.adapter.base.BaseQuickAdapter
import com.google.gson.Gson
import com.lzy.okgo.OkGo
import com.lzy.okgo.callback.StringCallback
import com.lzy.okgo.model.Response
import kotlinx.android.synthetic.main.activity_main.*
import org.json.JSONObject

class JobActivity : AppCompatActivity() {
    var move = ""
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        initView()
    }


    fun initView() {
//        recycler.addItemDecoration(DividerItemDecoration(this@JobActivity, LinearLayout.VERTICAL))
        recycler.layoutManager = LinearLayoutManager(this@JobActivity) as RecyclerView.LayoutManager?
//        smartRefresh!!.autoRefresh()
//        recycler.itemAnimator = DefaultItemAnimator()
        //解决recyclerView与NestedscrollView嵌套的滚动卡顿问题
        recycler.isNestedScrollingEnabled = false

        recycler.addItemDecoration(SpacesItemDecoration(30))



        recycler_suggest.layoutManager = LinearLayoutManager(this@JobActivity)
//        smartRefresh!!.autoRefresh()
//        recycler.itemAnimator = DefaultItemAnimator()
        //解决recyclerView与NestedscrollView嵌套的滚动卡顿问题
        recycler_suggest.isNestedScrollingEnabled = false
        recycler_suggest.addItemDecoration(SpacesItemDecoration(30))

        keywords.addTextChangedListener(object : TextWatcher {
            override fun afterTextChanged(p0: Editable?) {
                if (!StringUtils.isTrimEmpty(keywords.text.toString())) {
                    if (move == "") {
                        getSuggest()
                        Log.e("------","---dongle")
                    } else {
                        move = ""
                        Log.e("------","---meiyou")

                    }

                } else {
                    recycler_suggest.visibility = View.GONE
                }

            }

            override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
            }

            override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
            }

        })
        search.setOnClickListener {
            getData()
        }


    }

    fun getData() {
        OkGo.get<String>(Constant.JOBURL)
                .params("kw", keywords.text.toString())
                .execute(object : StringCallback() {
                    override fun onSuccess(response: Response<String>) {
                        println(response.body())
                        val jsonObject = JSONObject(response.body())
                        if (jsonObject.getInt("code") == 200) {
                            val gson = Gson().fromJson(response.body(), Jobs::class.java)
                            var adapter = JobAdpater(R.layout.item_job, gson.data.results)
                            recycler.adapter = adapter
                            adapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN)
                        }

                    }
                })
    }

    fun getSuggest() {
        OkGo.get<String>(Constant.JOBSUGGEST)
                .params("keyWord", keywords.text.toString())
                .execute(object : StringCallback() {
                    override fun onSuccess(response: Response<String>) {
                        println(response.body())
                        val jsonObject = JSONObject(response.body())
                        if (jsonObject.getInt("code") == 200) {
                            val gson = Gson().fromJson(response.body(), JobSuggest::class.java)
                            var adapter = JobSuggestAdpater(R.layout.item_suggest, gson.data.results)
                            recycler_suggest.visibility = View.VISIBLE
                            recycler_suggest.adapter = adapter

                            adapter.setOnItemClickListener { adapter, view, position ->
                                val tv_content = view.findViewById<TextView>(R.id.jobname)
                                Log.e("关键词", "======${tv_content.text}")
                                move = "yes"
                                keywords.setText(tv_content.text)
                                recycler_suggest.visibility = View.GONE
                            }

                        }

                    }
                })
    }
}